(require (planet schematics/schemeunit:3))
; f(n) =
;      n                              se n < 3
;      f(n-1) + 2*f(n-2) + 3*f(n-3)   se n >= 3

; Processo recursivo
(define (f-rec n)
  (if (< n 3)
      n
      (+ (f-rec (- n 1))
         (* 2 (f-rec (- n 2)))
         (* 3 (f-rec (- n 3))))))

; Processo iterativo
(define (f-it n)
  (f-it- 0 1 2 n))
(define (f-it- c b a n)
  (if (= n 0)
      c
      (f-it- b a (+ a (* 2 b) (* 3 c)) (- n 1))))

; Alguns testes
(check-equal? (f-rec 0) 0)
(check-equal? (f-rec 1) 1)
(check-equal? (f-rec 2) 2)
(check-equal? (f-rec 3) 4)
(check-equal? (f-rec 4) 11)
(check-equal? (f-rec 5) 25)
(check-equal? (f-rec 0) (f-it 0))
(check-equal? (f-rec 7) (f-it 7))
(check-equal? (f-rec 14) (f-it 14))